package com.arlen.security_oauth2_1.conf;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices;
import org.springframework.security.oauth2.provider.code.JdbcAuthorizationCodeServices;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

import javax.sql.DataSource;

/**
 * 授权服务器配置
 */
@SuppressWarnings("all")
@Order(2)
@Configuration
@EnableAuthorizationServer
public class OAuthConf extends AuthorizationServerConfigurerAdapter {

    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;


    @Bean
    public ClientDetailsService jdbcClientDetailsService(){
        //将client信息存储在数据库中
        return new JdbcClientDetailsService(dataSource);
    }


    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        /*        //配置存储类型
        clients.jdbc(dataSource)
                //客户端id
                .withClient("admin")
                //密码
                .secret(bCryptPasswordEncoder.encode("123"))
                //重定向地址
                .redirectUris("http://www.baidu.com")
                //授权类型 授权码模式
                .authorizedGrantTypes("authorization_code")
                //作用域
                .scopes("all");*/
        //设置客户端的配置从数据库中读取，存储在oauth_client_details表
        clients.withClientDetails(jdbcClientDetailsService());

    }


    private static final DruidDataSource dataSource ;
    @Value("${spring.datasource}")
    private static String dviver;
    static {
        dataSource = new DruidDataSource();
        dataSource.setDriverClassName(dviver);
        dataSource.setUrl("jdbc:mysql://rm-bp103z5076g14z0dbao.mysql.rds.aliyuncs.com/arlen?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT");
        dataSource.setUsername("root");
        dataSource.setPassword("Ymz2580@");
    }

    @Bean
    public TokenStore tokenStore(){
       // tokrn的存储方式 通过数据库进行存储token   数据存储在oauth_access_token和oauth_refresh_token
        return new JdbcTokenStore(dataSource);
    }

    @Bean
    public AuthorizationCodeServices authorizationCodeServices() {
        //加入对授权码模式的支持
        return new JdbcAuthorizationCodeServices(dataSource);
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.tokenStore(tokenStore());



    }
}
